
import preferences from '@ohos.data.preferences'
class PreferenceUtil {

  private pre:preferences.Preferences


  /**
   * 加载首选项
   * @param context 上下文
   */
  async initPreference(context, name:string){
    this.pre = await preferences.getPreferences(context,name)
  }

  /**
   * 获取全部内容
   */
  async getPreference(key:string){
    let value:string = await this.pre.get(key,"") as string
    return value;
  }

  /**
   * 存储Value
   * @param key
   * @param value
   */
  async putPreference(key:string,value:preferences.ValueType){
    await this.pre.put(key,value)
    this.pre.flush()
    let res = await this.pre.get(key,'defaultValue')
    console.log("添加结果：",res)
  }

  /**
   * 删除
   * @param key
   */
  async delPreference(key:string){
    this.pre.delete(key)
    this.pre.flush()
  }
}

let p = new PreferenceUtil()

export default p as PreferenceUtil